﻿using System;

namespace TSharp.Core.Web.Message.MessageImpl
{
    /// <summary>
    /// 消息注册句柄实现
    /// <para>by tangjingbo at 2009/12/14 21:38</para>
    /// </summary>
    /// <typeparam name="TMessage">The type of the message.</typeparam>
    internal class MessageListenerRegHandleImpl<TMessage> : IMessageListenerRegHandle<TMessage>
    {
        public MessageListenerRegHandleImpl(MessageListener<TMessage> listener)
        {
            this.listener = listener;
            Register();
        }

        #region IMessageListenerRegHandle<TMessage> 成员

        private static readonly Type messageType = typeof (TMessage);
        private readonly MessageListener<TMessage> listener;
        private bool enabled = true;
        private int order = -1;

        public MessageListener<TMessage> GetListener()
        {
            return listener;
        }

        public void UnRegister()
        {
            Registered = false;
            MessageListenerManage.RemoveHandler(this);
        }

        public bool Registered { get; internal set; }

        public Type MessageType
        {
            get { return messageType; }
        }

        public bool Enabled
        {
            get { return enabled; }
            set { enabled = value; }
        }

        public bool IsListenSubMessage { get; set; }

        public bool IsListenBubble { get; set; }

        public bool IsListenBroadcast { get; set; }

        public int Order
        {
            get { return order; }
            internal set { order = value; }
        }

        private void Register()
        {
            MessageListenerManage.AddHandler(this);
            Registered = true;
        }

        #endregion
    }
}